package edu.utdallas_elearn_calendar.utdallas_elearn_calendar_app;

import edu.utdallas_elearn_calendar.utdallas_elearn_calendar_db.CalDbAdapter;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
//import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class event_types extends ListActivity {
	// not sure what these do or why they are needed
	private static final int INSERT_EVENT_TYPE = 0;
	private static final int EDIT_EVENT_TYPE = 1;
	private static final int DELETE_ID = Menu.FIRST + 1;
	private Cursor cursor;
	private CalDbAdapter dbHelper;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.empty_list);
        this.getListView().setDividerHeight(2);
        dbHelper = new CalDbAdapter(this);
		dbHelper.open();
		fillList();
		registerForContextMenu(getListView());	
    }
    
	// Create the menu based on the XML defintion
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.add_event_type_menu, menu);
		return true;
	}
	
	// Reaction to the menu selection
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.add_event_type:
			editEventType();
			return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.add_event_type:
			editEventType();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void editEventType() {
		Intent i = new Intent(this, event_type_edit.class);
		startActivityForResult(i, INSERT_EVENT_TYPE);
	}
	
	// Action to take when a particular list item is clicked
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
//		Toast.makeText(this, "Edit field is " + CalDbAdapter.ROW_ID + " = " + id, Toast.LENGTH_LONG).show();
		Intent i = new Intent(this, event_type_edit.class);
		i.putExtra(CalDbAdapter.ROW_ID, id);
		
		// Activity returns a result if called with startActivityForResult		
		startActivityForResult(i, EDIT_EVENT_TYPE);
	}	
    
	// Called by startActivityForResult
	// with the result of the activity
	// requestCode is the original request code sent to the activity
	// resultCode is the return code, 0 if everything is ok
	// intent can be use to get some data from the caller
	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
//		Toast.makeText(this, "The request code is " + requestCode + ".", Toast.LENGTH_LONG).show();
		fillList();
	}
	
	private void fillList() {
		cursor = dbHelper.getAllEventType();
		startManagingCursor(cursor);

//		Toast.makeText(this, "The number of records is " + cursor.getCount() + ".", Toast.LENGTH_LONG).show();
		String[] from = new String[] { CalDbAdapter.EVENT_TYPE };
		int[] to = new int[] { R.id.event_type };
		
		// Now create an array adapter and set it to display using the row
		SimpleCursorAdapter eventTypeInfo = new SimpleCursorAdapter(this,
				R.layout.event_types, cursor, from, to);
		setListAdapter(eventTypeInfo);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		
		menu.setHeaderTitle(R.string.event_type_delete);
		menu.add(0, DELETE_ID, 0, R.string.menu_delete);
		menu.add(0, DELETE_ID+1, 0, R.string.menu_cancel);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case DELETE_ID:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
					.getMenuInfo();
			dbHelper.deleteClass(info.id);
			fillList();
			return true;
		case DELETE_ID+1:
			return false;
		}
		return super.onContextItemSelected(item);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		//dbHelper.close();
	}

	@Override
	protected void onResume() {
		super.onResume();
		//dbHelper.open();
		fillList();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (dbHelper != null) {
			dbHelper.close();
		}
	}		
}